<?php
namespace app\common\model;

use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class User extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * 获取列表
     * @param $field
     * @param $where
     * @param $limit
     * @param $page
     * @param $status
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getList($data = array())
    {
            if(!empty($data)){
                if(!empty($data['alias']))$this->alias($data['alias']);
                if(!empty($data['field']))$this->field($data['field']);
                if(!empty($data['order']))$this->order($data['order']);
                if(!empty($data['where']))$this->where($data['where']);
                if(!empty($data['join']))$this->join($data['join']);
                if(!empty($data['page']) && !empty($data['limit']))$this->page($data['page'],$data['limit']);
                $role_data = $this->select();
                return $role_data;
            }else{
                return '数据不存在';
            }
    }

    public function getOneData($data)
    {
        if (!empty($data)) {
            if (!empty($data['alias'])) $this->alias($data['alias']);
            if (!empty($data['field'])) $this->field($data['field']);
            if (!empty($data['order'])) $this->order($data['order']);
            if (!empty($data['where'])) $this->where($data['where']);
            if (!empty($data['join'])) $this->where($data['join']);
            $role_data = $this->find();
            return $role_data;
        } else {
            return '数据不存在';
        }
    }

    /**
     * 添加新用户
     * @param $data array
     * @return bool
     * @throws \think\Exception
     */
    public function addUser($data)
    {
        # 开启事务
        $this->startTrans();
        $addRes = $this->allowField(true)->data($data)->save();

        # 查询p_u_id的层级链，加上自己的id
        $pUIdWhere = ['u_id' => $data['p_u_id']];
        $pUIdInfo = $this->where($pUIdWhere)->field('u_level_line,p_u_id')->find();

        $updateRes = $this->isUpdate(true)->save(['u_level_line' => $pUIdInfo['u_level_line'].'-'.$this->u_id]);


        # 记录器
        $recorder = [];


        if ($addRes && $updateRes){
            # 如果记录器中有一条失败则回滚
            foreach ($recorder as $value){
                if (!$value || $value == 0) {
                    $this->rollback();
                    return false;
                }
            }
            $this->commit();
            return true;
        }else{
            $this->rollback();
            return false;
        }
    }

    /**
     * 编辑用户资料
     * @param $data array
     * @return bool
     * @throws \think\Exception
     */
    public function editUser($data)
    {
        if(empty($data)){
           return false;
        }

        $addRes = $this->allowField(true)->save($data['data'],$data['where']);

        if($addRes){
            return true;
        }

        return false;
    }

    /**
     * 删除用户
     * @param $uId
     * @return bool
     * @throws \think\Exception
     */
    public function del($uId)
    {
        # 开启事务
        $this->startTrans();
        # 更改用户状态
        $uInfo = $this->get($uId);

        $uInfo->u_status = '-1';
        $uRes = $uInfo->save();

        # 记录器
        $recorder = [];

        # 上级子用户数量-1

        if ($uRes) {
            $this->commit();
            return true;
        }else{
            $this->rollback();
            return false;
        }
    }
}